﻿
#region Imports...
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using TicketManager.Data;
using TicketManager.Entities;
using System.IO;
using System.Data.SqlClient;
#endregion

public partial class PhimEdit : System.Web.UI.Page
{
    string phimId = "";
	protected void Page_Load(object sender, EventArgs e)
	{		
        //FormUtil.RedirectAfterInsertUpdate(FormView1, "PhimEdit.aspx?{0}", PhimDataSource);
        //FormUtil.RedirectAfterAddNew(FormView1, "PhimEdit.aspx");
        //FormUtil.RedirectAfterCancel(FormView1, "Phim.aspx");
        //FormUtil.SetDefaultMode(FormView1, "PhimId");
        if (Request.QueryString["PhimId"] != null && Request.QueryString["PhimId"] != "")
        {
            phimId = Convert.ToString(Request.QueryString["PhimId"]);
        }

        if (!IsPostBack)
        {           
            if (!string.IsNullOrEmpty(phimId))
            {
                BindData(phimId);
            }
            SetTextControl();
        }
	}

    protected void SetTextControl()
    {
        if (phimId != string.Empty)
        {
            btnUpdate.Text = "Cập nhật";
        }
        else
        {
            btnUpdate.Text = "Thêm";
        }
    }

    #region btnUpdate_OnClick
    protected void btnUpdate_OnClick(object sender, EventArgs e)
    {
        SaveData(phimId);
    }
    #endregion

    #region BindData
    protected void BindData(string pId)
    {
        TicketManager.Entities.Phim p = null;
        if (!string.IsNullOrEmpty(pId))
        {
            p = DataRepository.PhimProvider.GetByPhimId(Convert.ToInt32(pId));
            if (p!=null)
            {
                dataTenPhim.Text = p.TenPhim;
                dataLichChieuBatDau.Text = Convert.ToDateTime(p.LichChieuBatDau).ToString("MM-dd-yyyy");
                dataLichChieuKetThuc.Text = Convert.ToDateTime(p.LichChieuKetThuc).ToString("MM-dd-yyyy");
                dataMoTa.Text = p.MoTa;
                if (p.TrangThai)
                    dataTrangThai.SelectedIndex = 0;
                else
                    dataTrangThai.SelectedIndex = 1;                
            }
        }        
    }
    #endregion

    #region SaveData
    protected void SaveData(string pId)
    {
        try
        {
            // Case update
            TicketManager.Entities.Phim p = null;
            if (!string.IsNullOrEmpty(pId))
            {
                p = DataRepository.PhimProvider.GetByPhimId(Convert.ToInt32(pId));
            }
            else
            {
                p = new TicketManager.Entities.Phim();
            }

            SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["TicketManager.Data.ConnectionString"].ConnectionString);            
            string anhMoTa = "";
            if (fuAnh.HasFile)
            {
                string folderUpload = Server.MapPath("~/Admin/Upload");
                HttpPostedFile postFile = fuAnh.PostedFile;
                string fileName = postFile.FileName;
                if (File.Exists(string.Format(@"{0}\{1}", folderUpload, fileName)))
                    File.Delete(string.Format(@"{0}\{1}", folderUpload, fileName));
                postFile.SaveAs(string.Format(@"{0}\{1}", folderUpload, fileName));
                anhMoTa = "~/Admin/Upload/" + fileName;
            }

            bool bTrangThai = false;
            if (dataTrangThai.SelectedValue == "True")
                bTrangThai = true;
            else
                bTrangThai = false;
            string sqlUpdate = string.Format("UPDATE [dbo].[phim] SET [TenPhim] =N'{0}' ,[LichChieuBatDau] ='{1}' ,[MoTa] = N'{2}' ,[TrangThai] = '{3}',[LichChieuKetThuc] = '{4}',[AnhMoTa] ='{5}'  WHERE [PhimID] = '{6}'",
                dataTenPhim.Text, Convert.ToDateTime(dataLichChieuBatDau.Text), dataMoTa.Text, bTrangThai, Convert.ToDateTime(dataLichChieuKetThuc.Text), anhMoTa,phimId);
            if (anhMoTa == "")
            {
                sqlUpdate = string.Format("UPDATE [dbo].[phim] SET [TenPhim] =N'{0}' ,[LichChieuBatDau] ='{1}' ,[MoTa] = N'{2}' ,[TrangThai] = '{3}',[LichChieuKetThuc] = '{4}'  WHERE [PhimID] = '{5}'",
                dataTenPhim.Text, Convert.ToDateTime(dataLichChieuBatDau.Text), dataMoTa.Text, bTrangThai, Convert.ToDateTime(dataLichChieuKetThuc.Text), phimId);
            }
            string sqlInsert=string.Format("INSERT INTO [dbo].[phim]"+
					"("+
					"[TenPhim]"+
					",[LichChieuBatDau]"+
					",[MoTa]"+
					",[TrangThai]"+
					",[LichChieuKetThuc]"+
					",[AnhMoTa]"+
					")"+
				"VALUES"+
					"("+
					"N'{0}'"+
					",'{1}'"+
					",N'{2}'"+
					",'{3}'"+
					",'{4}'"+
					",'{5}'"+
                    ")", dataTenPhim.Text, Convert.ToDateTime(dataLichChieuBatDau.Text), dataMoTa.Text, bTrangThai, Convert.ToDateTime(dataLichChieuKetThuc.Text), anhMoTa);
            //string sql = string.Format("UPDATE [dbo].[phim] SET [TenPhim] ='{0}' ,[LichChieuBatDau] ='{1}' ,[MoTa] = '{2}' ,[TrangThai] = '{3}',[LichChieuKetThuc] = '{4}',[AnhMoTa] ='{5}'  WHERE [PhimID] = '{6}'");

            //p.TenPhim = dataTenPhim.Text;
            //p.LichChieuBatDau = Convert.ToDateTime(dataLichChieuBatDau.Text);
            //p.LichChieuKetThuc = Convert.ToDateTime(dataLichChieuKetThuc.Text);
            //p.MoTa = dataMoTa.Text;
            //if (dataTrangThai.SelectedValue == "True")
            //    p.TrangThai = true;
            //else
            //    p.TrangThai = false;

            SqlCommand commd;
            if (!string.IsNullOrEmpty(pId))
            {
                //DataRepository.PhimProvider.Update(p);
                commd = new SqlCommand(sqlUpdate, conn);
            }
            else
            {
                //DataRepository.PhimProvider.Insert(p);
                commd = new SqlCommand(sqlInsert, conn);
            }
            if (conn.State.Equals(ConnectionState.Closed))
                conn.Open();
            commd.ExecuteNonQuery();


            Response.Redirect("~/Admin/Phim.aspx");
        }
        catch (Exception ex)
        {
            Response.Write(ex.ToString());
        }
    }

    #endregion

    protected void GridViewVe1_SelectedIndexChanged(object sender, EventArgs e)
	{
        //string urlParams = string.Format("VeId={0}", GridViewVe1.SelectedDataKey.Values[0]);
        //Response.Redirect("VeEdit.aspx?" + urlParams, true);		
	}	
}


